package dissertation.extensions.ejbmodule.control;

import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.libraries.LibraryKind;
import com.intellij.openapi.roots.libraries.NewLibraryConfiguration;
import com.intellij.openapi.roots.ui.configuration.libraries.CustomLibraryDescription;
import com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryEditor;
import com.intellij.openapi.vfs.VirtualFile;
import dissertation.common.constants.Constants;
import dissertation.extensions.ejbmodule.view.EJBFileChooserDescriptor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.Collections;
import java.util.Set;

public class EJBLibraryDescription extends CustomLibraryDescription {
    @NotNull
    @Override
    public Set<? extends LibraryKind<?>> getSuitableLibraryKinds() {
        return Collections.singleton(LibraryKind.create(Constants.EJB_ID));
    }

    @Override
    public NewLibraryConfiguration createNewLibrary(@NotNull JComponent parentComponent, @Nullable VirtualFile virtualFile) {
        NewLibraryConfiguration result = null;
        FileChooserDescriptor descriptor = new EJBFileChooserDescriptor();
        final VirtualFile directory = FileChooser.chooseFile(parentComponent, descriptor);
        if (directory != null) {
            String path = directory.getPath();
            result = new NewLibraryConfiguration(path) {
                @Override
                public void addRoots(@NotNull LibraryEditor editor) {
                    editor.addJarDirectory(directory, true, OrderRootType.CLASSES);
                }
            };
        }

        return result;
    }
}
